// pages/msg_notice/msg_notice.js
let app = getApp();
var type = ''
var my_notice = ''
Page({

    /**
     * 页面的初始数据
     */
    data: {
        CustomBar: app.globalData.CustomBar,
        loadModal: true,
        screen: false,
        xianshi: false,
        dateright: '',
        dateleft: '',
        tihuoWay: '全部状态',
        xianshi2: false,
        xianshi4: true,
        select: false,
        xlk1: [
            [
                '全部状态',
                '完成',
                '暂存',
            ],
        ],
        searchdata: [],
        deliverplan: []
    },
    //时间选择器
    bindDateChangel: function (e) {
        this.setData({
            dateleft: e.detail.value
        })
    },
    bindDateChanger: function (e) {
        this.setData({
            dateright: e.detail.value
        })
    },
    //输入框的确定搜索
    bindconfirm: function (e) {
        var that = this
        console.log('获取到的输入框中的id：', e.currentTarget.dataset.id)
        // 货品信息页面的搜索
        if (that.data.searchdata == '') {
            that.setData({
                searchdata: that.data.deliverplan
            })
        }
        if (e.detail.value == '') {
            that.setData({
                deliverplan: that.data.searchdata
            })
        } else {
            var data = that.data.deliverplan
            let s = e.detail.value

            function aa() {
                let _this = that
                let str = data
                console.log(s)
                let searchid = [];
                let sear = new RegExp(s);
                for (var j = 0; j < str.length; j++) {
                    if (sear.test(str[j].LITEMNAME)) {
                        searchid.push(str[j]);
                        console.log(searchid)
                    }
                }
                console.log(searchid)
                if (searchid != '') {
                    _this.setData({
                        deliverplan: searchid
                    })
                    wx.showToast({
                        title: '搜索成功',
                        icon: 'success',
                        duration: 1000
                    })
                } else {
                    wx.showToast({
                        title: '未搜索到相关信息',
                        icon: 'none',
                        duration: 2000,
                        mask: 'false'
                    })
                    _this.setData({
                        deliverplan: searchid
                    })
                }
            };
            aa();
        }
        console.log('搜索后赋值的列表：', that.data.goodsdata)

    },
    scan1() { // 添加生产通知单
        wx.navigateTo({
            url: '../msg_notice_add/msg_notice_add'
        })
    },
    jump1(e) {
        wx.navigateTo({
            url: '../msg_canvas/msg_canvas?code=' + e.currentTarget.dataset.lid
        })
    },
    contract(e) { // 跳转生产通知单详情页面
        wx.navigateTo({
            url: '../msg_notice_details/msg_notice_details?lid=' + e.currentTarget.dataset.lid + '&lstaterun=' + e.currentTarget.dataset.lstaterun
        })
    },
    updateData1() { //事件搜索
        this.setData({
            loadModal: true,
        })
        my_notice()
    },
    binditem(e) {
        var that = this
        console.log("当前仓库类型为" + ":" + e.target.dataset.index)
        var name = e.currentTarget.dataset.name
        that.setData({
            tihuoWay: name,
            select: false,
        })
        if (name == '完成') {
            type = 4
        } else if (name == '暂存') {
            type = 1
        } else if (name == '全部状态') {
            type = 0
        }
        my_notice()
    },

    //-下拉框--
    bindmsg() {
        this.setData({
            select: !this.data.select,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        app.globalData.number = 0
        type = 0
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        let s = new Date(new Date().setDate(new Date().getDate() - 30)).toLocaleString().substring(0, 10).toLocaleString()

        var lw = new Date(date - 1000 * 60 * 60 * 24 * 30); //最后一个数字30可改，30天的意思
        var lastY = lw.getFullYear();
        var lastM = lw.getMonth() + 1;
        var lastD = lw.getDate();
        var startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD); //三十天之前日期
        // console.log(startdate)
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        var dateright = year + "-" + month + "-" + day;

        that.setData({
            dateright: dateright
        })
        that.setData({
            dateleft: startdate
        })
        function notice() {
            wx.request({
                url: 'https://' + app.globalData.url + '/Contract/findContract',
                data: {
                    startTime: that.data.dateleft,
                    endTime: that.data.dateright,
                    TYPE: type,
                    lusercode: app.globalData.lusercode
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/json;charset=utf-8'
                },
                success: function (res) {
                    if (res.data.success) {
                        if (res.data.obj != '' && res.data.obj != null) {
                            that.setData({
                                loadModal: false,
                                deliverplan: res.data.obj,
                              xianshi:false
                            })
                        } else {
                            that.setData({
                                loadModal: false,
                                xianshi: true
                            })
                            // wx.showModal({
                            //     title: '提示',
                            //     content: '当前数据为空',
                            //     showCancel: false,
                            //     confirmText: '确定',
                            //     success(res) {
                            //         if (res.confirm) {

                            //         }
                            //     }
                            // })
                        }
                    } else {
                        that.setData({
                            loadModal: false,
                            xianshi: true
                        })
                        wx.showModal({
                            title: '提示',
                            content: '获取数据失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {

                                }
                            }
                        })
                    }

                },
                fail: function (res) {
                    that.setData({
                        loadModal: false,
                        xianshi: true
                    })
                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                },
            })
        }
        notice()
        my_notice = notice
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (app.globalData.number > 0) {
            my_notice()
        }
        app.globalData.number++
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})